{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ecg_fractal_analysis is intendeed to create pickle datasets in the form: normal ecg signals x 12 ecg derivations, containig the values of the corresponding complexity measures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load requiered packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import h5py\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import neurokit2 as nk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read normal_ecg_age dataframe containing normal ecg id's and corresponding age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "normal_ecg_age = pd.read_pickle('normal_ecg_age.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Age clases based on intervals define on the age feature with labels from 0: [10, 20), 1: [20, 30), 2: [30, 40), 3: [40, 50), 4: [50, 60), 5: [60, 70), 6: [70, 80), 7: [80, 90), 8: [90, 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ECG_ID</th>\n",
       "      <th>Age</th>\n",
       "      <th>Age_class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>63</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>46</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>48</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ECG_ID  Age  Age_class\n",
       "0  A00002   32          2\n",
       "1  A00003   63          5\n",
       "2  A00006   46          3\n",
       "3  A00008   32          2\n",
       "4  A00009   48          3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "normal_ecg_age.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Selecting signals length for fracta analysis in order to standarized calculations due to fractal features are related with signal's length"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Including in the dataframe signals length requiered for complexity calculations. If OSError then signal's length is set to NaN. Needed for standarizing signals length because complexity measures depends on signal's length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "length_dataframe = pd.DataFrame(columns=['ID', 'I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'])\n",
    "temp_length = []\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r') as f:\n",
    "        for der in range(12):\n",
    "            try:\n",
    "                temp_length.append(len(f['ecg'][der]))\n",
    "            except OSError:\n",
    "                temp_length.append(np.NaN)\n",
    "                continue\n",
    "    temp_length.insert(0, id)\n",
    "    length_dataframe.loc[len(length_dataframe)] = temp_length\n",
    "    temp_length = []          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>I</th>\n",
       "      <th>II</th>\n",
       "      <th>III</th>\n",
       "      <th>aVR</th>\n",
       "      <th>aVL</th>\n",
       "      <th>aVF</th>\n",
       "      <th>V1</th>\n",
       "      <th>V2</th>\n",
       "      <th>V3</th>\n",
       "      <th>V4</th>\n",
       "      <th>V5</th>\n",
       "      <th>V6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "      <td>6500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "      <td>6000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       ID     I    II   III   aVR   aVL   aVF    V1    V2    V3    V4    V5  \\\n",
       "0  A00002  6000  6000  6000  6000  6000  6000  6000  6000  6000  6000  6000   \n",
       "1  A00003  6500  6500  6500  6500  6500  6500  6500  6500  6500  6500  6500   \n",
       "2  A00006  5000  5000  5000  5000  5000  5000  5000  5000  5000  5000  5000   \n",
       "3  A00008  5000  5000  5000  5000  5000  5000  5000  5000  5000  5000  5000   \n",
       "4  A00009  6000  6000  6000  6000  6000  6000  6000  6000  6000  6000  6000   \n",
       "\n",
       "     V6  \n",
       "0  6000  \n",
       "1  6500  \n",
       "2  5000  \n",
       "3  5000  \n",
       "4  6000  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "length_dataframe.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create normal_ecg_length dataframe containing the derivations lenghts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "length_dataframe.to_pickle('normal_ecg_length.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Verifying missing data in dataframe. NaN values related with OSError in signals reading. Solution: download SPH dataset again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID     0\n",
       "I      0\n",
       "II     0\n",
       "III    0\n",
       "aVR    0\n",
       "aVL    0\n",
       "aVF    0\n",
       "V1     0\n",
       "V2     0\n",
       "V3     0\n",
       "V4     0\n",
       "V5     0\n",
       "V6     0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "length_dataframe.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Minimun and maximun signal lengths. Signal length set to 5000 for all derivations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <td>A00002</td>\n",
       "      <td>A25765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>II</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>III</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>aVR</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>aVL</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>aVF</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>V1</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>V2</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>V3</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>V4</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>V5</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>V6</th>\n",
       "      <td>5000</td>\n",
       "      <td>17500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0       1\n",
       "ID   A00002  A25765\n",
       "I      5000   17500\n",
       "II     5000   17500\n",
       "III    5000   17500\n",
       "aVR    5000   17500\n",
       "aVL    5000   17500\n",
       "aVF    5000   17500\n",
       "V1     5000   17500\n",
       "V2     5000   17500\n",
       "V3     5000   17500\n",
       "V4     5000   17500\n",
       "V5     5000   17500\n",
       "V6     5000   17500"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([length_dataframe.min(), length_dataframe.max()], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Complexity Katz's fractal dimension measure "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "Katz_dataframe = pd.DataFrame(columns=['ID', 'I_Katz', 'II_Katz', 'III_Katz', 'aVR_Katz', 'aVL_Katz', 'aVF_Katz', 'V1_Katz', 'V2_Katz', 'V3_Katz', 'V4_Katz', 'V5_Katz', 'V6_Katz'])\n",
    "kfd_temp = []\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "        with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r') as f:\n",
    "            for der in range(12):\n",
    "                try:\n",
    "                    kfd, _ = nk.fractal_katz(f['ecg'][der][:5000])\n",
    "                    kfd_temp.append(kfd) \n",
    "                except OSError:\n",
    "                    kfd_temp.append(np.NaN)\n",
    "                    continue\n",
    "        kfd_temp.insert(0, id)            \n",
    "        Katz_dataframe.loc[len(Katz_dataframe)] = kfd_temp\n",
    "        kfd_temp = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "Katz_dataframe.to_pickle('normal_ecg_katz.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Complexity LL's fractal dimension measure "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "LL_dataframe = pd.DataFrame(columns=['ID', 'I_LL', 'II_LL', 'III_LL', 'aVR_LL', 'aVL_LL', 'aVF_LL', 'V1_LL', 'V2_LL', 'V3_LL', 'V4_LL', 'V5_LL', 'V6_LL'])\n",
    "LL_temp = []\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "        with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r') as f:\n",
    "            for der in range(12):\n",
    "                try:\n",
    "                    LL, _ = nk.fractal_linelength(f['ecg'][der][:5000])\n",
    "                    LL_temp.append(LL) \n",
    "                except OSError:\n",
    "                    LL_temp.append(np.NaN)\n",
    "                    continue\n",
    "        LL_temp.insert(0, id)            \n",
    "        LL_dataframe.loc[len(LL_dataframe)] = LL_temp\n",
    "        LL_temp = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "LL_dataframe.to_pickle('normal_ecg_ll.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Complexity Petrosian's fractal dimension measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "Pt_dataframe = pd.DataFrame(columns=['ID', 'I_Pt', 'II_Pt', 'III_Pt', 'aVR_Pt', 'aVL_Pt', 'aVF_Pt', 'V1_Pt', 'V2_Pt', 'V3_Pt', 'V4_Pt', 'V5_Pt', 'V6_Pt'])\n",
    "Pt_temp = []\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "        with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r') as f:\n",
    "            for der in range(12):\n",
    "                try:\n",
    "                    Pt, _ = nk.fractal_petrosian(f['ecg'][der][:5000], symbolize='C', show=False)\n",
    "                    Pt_temp.append(Pt) \n",
    "                except OSError:\n",
    "                    Pt_temp.append(np.NaN)\n",
    "                    continue\n",
    "        Pt_temp.insert(0, id)            \n",
    "        Pt_dataframe.loc[len(Pt_dataframe)] = Pt_temp\n",
    "        Pt_temp = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "Pt_dataframe.to_pickle('normal_ecg_petrosian.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Complexity Sevcik's fractal dimension measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sk_dataframe = pd.DataFrame(columns=['ID', 'I_Sk', 'II_Sk', 'III_Sk', 'aVR_Sk', 'aVL_Sk', 'aVF_Sk', 'V1_Sk', 'V2_Sk', 'V3_Sk', 'V4_Sk', 'V5_Sk', 'V6_Sk'])\n",
    "Sk_temp = []\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "        with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r') as f:\n",
    "            for der in range(12):\n",
    "                try:\n",
    "                    Sk, _ = nk.fractal_sevcik(f['ecg'][der][:5000])\n",
    "                    Sk_temp.append(Sk)\n",
    "                except OSError:\n",
    "                    Sk_temp.append(np.NaN)\n",
    "                    continue\n",
    "        Sk_temp.insert(0, id)            \n",
    "        Sk_dataframe.loc[len(Sk_dataframe)] = Sk_temp\n",
    "        Sk_temp = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sk_dataframe.to_pickle('normal_ecg_sevcik.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Complexity NLD's fractal dimension measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\lib\\nanfunctions.py:1739: RuntimeWarning: overflow encountered in multiply\n",
      "  sqr = np.multiply(arr, arr, out=arr, where=where)\n",
      "c:\\Users\\YANIBIS\\ecg_biomarkers\\lib\\site-packages\\numpy\\core\\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce\n",
      "  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'sig' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 16\u001b[0m\n\u001b[0;32m     13\u001b[0m         NLD_dataframe\u001b[39m.\u001b[39mloc[\u001b[39mlen\u001b[39m(NLD_dataframe)] \u001b[39m=\u001b[39m NLD_temp\n\u001b[0;32m     14\u001b[0m         NLD_temp \u001b[39m=\u001b[39m []\n\u001b[1;32m---> 16\u001b[0m nk\u001b[39m.\u001b[39mfractal_nld(sig, corrected\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'sig' is not defined"
     ]
    }
   ],
   "source": [
    "NLD_dataframe = pd.DataFrame(columns=['ID', 'I_NLD', 'II_NLD', 'III_NLD', 'aVR_NLD', 'aVL_NLD', 'aVF_NLD', 'V1_NLD', 'V2_NLD', 'V3_NLD', 'V4_NLD', 'V5_NLD', 'V6_NLD'])\n",
    "NLD_temp = []\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "        with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r') as f:\n",
    "            for der in range(12):\n",
    "                try:\n",
    "                    NLD, _ = nk.fractal_nld(f['ecg'][der][:5000], corrected=False)\n",
    "                    NLD_temp.append(NLD)\n",
    "                except OSError:\n",
    "                    NLD_temp.append(np.NaN)\n",
    "                    continue\n",
    "        NLD_temp.insert(0, id)            \n",
    "        NLD_dataframe.loc[len(NLD_dataframe)] = NLD_temp\n",
    "        NLD_temp = []\n",
    "\n",
    "nk.fractal_nld(sig, corrected=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "NLD_dataframe.to_pickle('normal_ecg_nld.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Complexity XX's fractal dimension measure"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 ('ecg_biomarkers': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d39f5264c2f26cb0964d5a98dbbf3a9d2bc7301339817434e4f0ffa82f5f047f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
